<template>
  <el-form ref="deptForm" label-width="120px" :rules="rules" :model="form">
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="$emit('success')">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments } from '@/api/department'
export default {
  name: 'DeptDialog',
  props: {
    pid: {
      type: String,
      required: true
    },
    originList: { type: Array, required: true }
  },
  data() {
    const validCode = (rule, value, callback) => {
      if (this.originList.some((item) => item.code === value)) {
        return callback(new Error(`已存在${value}部门编码`))
      }
      callback()
    }
    const validName = (rule, value, callback) => {
      if (
        this.originList.some(
          (item) => item.pid === this.pid && item.name === value
        )
      ) {
        return callback(new Error(`已存在${value}部门`))
      } else {
        callback
      }
    }
    return {
      list: [], // 部门负责人列表
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门不能为空', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: validCode,
            trigger: 'blur'
          }
        ],
        manager: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: 'blur'
          }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '部门介绍要求1-300个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.loadEmployee()
  },
  methods: {
    async hSubmit() {
      await this.$refs.deptForm.validate()
      await addDepartments({ ...this.form, pid: this.pid })
      this.$emit('success')
      this.$message.success('操作成功')
    },
    async loadEmployee() {
      const { data } = await getEmployeeSimple()
      this.list = data
    },
    close() {
      this.form = {}
      this.$refs.deptForm.clearValidate()
    }
  }
}
</script>

<style lang="less" scoped></style>
